/*
 * Copyright (C) 2007-2015 FBReader.ORG Limited <contact@fbreader.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.geometerplus.android.fbreader;

import android.app.ProgressDialog;
import android.util.Log;

import org.geometerplus.android.fbreader.util.BookSearch;
import org.geometerplus.android.fbreader.util.StringUtils;
import org.geometerplus.fbreader.fbreader.ActionCode;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.zlibrary.text.model.ZLTextMark;

class SearchAction extends FBAndroidAction {

	private ProgressDialog progressDialog;

	SearchAction(FBReader baseActivity, FBReaderApp fbreader) {
		super(baseActivity, fbreader);
	}

	@Override
	public boolean isVisible() {
		return Reader.Model != null;
	}

	@Override
	protected void run(Object ... params) {
		String param = "";
		if (params != null && params.length > 0) {
			param = (String) params[0];
		}
		runSearch(param);
	}

	public void runSearch(final String pattern) {

		final SearchPopup popup = (SearchPopup) Reader.getPopupById(ActionCode.SEARCH);
		Reader.showPopup(popup.getId());
		popup.setKeyword(pattern);

		if (StringUtils.isNullOrEmpty(pattern))
			return;

		popup.initPosition();

		Reader.MiscOptions.TextSearchPattern.setValue(pattern);
		popup.setOnItemClickListener(new SearchPopup.OnItemClickListener() {
			@Override
			public void onItemClick(ZLTextMark item) {
				Reader.getTextView().gotoPosition(item.ParagraphIndex, 0, 0);
				Reader.getTextView().gotoMark(item);
			}
		});

		BookSearch bookSearch = BookSearch.getInstance();
		progressDialog = new ProgressDialog(BaseActivity);
		progressDialog.setMessage("loading...");
		progressDialog.show();
		bookSearch.search(Reader.getTextView().getModel(), pattern, new BookSearch.BookSearchCallback() {

			@Override
			public void onFinishWithResult() {
				Log.d("booksearch", "onFinishWithResult: ");
				BaseActivity.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						popup.setData(Reader.Model.getTextModel().getMarks());
						Reader.getTextView().onSearchWithResult();
						finished();
					}
				});
			}

			@Override
			public void onFinishNoResult() {
				Log.d("booksearch", "onFinishNoResult: ");
				BaseActivity.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						popup.setData(null);
						popup.StartPosition = null;
						finished();
					}
				});
			}

			@Override
			public void onError(String msg) {
				BaseActivity.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						String errorMsg = "搜索出错了";
						Log.d("booksearch", errorMsg);
						popup.setData(null);
						popup.StartPosition = null;
						finished();
					}
				});
			}

			@Override
			public void onCanceled() {
				popup.StartPosition = null;
			}
		});
	}

	private void finished() {
		if (progressDialog != null && progressDialog.isShowing())
			progressDialog.dismiss();
	}
}
